import os
import shutil

# 定义文件夹路径
labels_folder = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\washed\Labels"
images_folder = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\washed\JPEGImages"
temp_folder = r"D:\上过的课\2024-2025-1-大四上\毕设\datasets\temp\JPEGImages"

# 确保 temp 文件夹存在
if not os.path.exists(temp_folder):
    os.makedirs(temp_folder)

# 遍历 labels 文件夹中的所有文件
for filename in os.listdir(labels_folder):
    if filename.endswith('.txt'):
        txt_file_path = os.path.join(labels_folder, filename)
        # 检查 txt 文件是否为空
        if os.path.getsize(txt_file_path) == 0:
            # 获取对应的 jpg 文件名
            image_name = os.path.splitext(filename)[0] + '.jpg'
            image_path = os.path.join(images_folder, image_name)
            # 检查对应的 jpg 文件是否存在
            if os.path.exists(image_path):
                # 复制 jpg 文件到 temp 文件夹
                shutil.copy2(image_path, temp_folder)
                print(f"Copied {image_name} to {temp_folder}")